import Vue from 'vue'
import Vuex from 'vuex'

import { userProfile } from '@/api/User'
// 引入获取主课程接口
import getClassifyr from '@/api/getclassifyr'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		token: '',  //登录成功后 token
		isCollapse: false,  // 控制菜单栏收缩
		dialogVisible: false, // 控制新增视频弹出框
		addDialogVisible: false, // 控制编辑弹出框
		getUser: {    //用户信息
			"username": "",
			"email": "",
			"phone": "",
			"avator": ""
		},
		area: '',  //部门
		pk: [] 	   // 一级标题pk
	},
	getters: {
	},
	mutations: {
		// 存储token
		getToken(state, value) {
			state.token = value
		},
		// 左侧菜单栏收缩展开
		changeI(state) {
			state.isCollapse = !state.isCollapse
		},
		// 保存用户信息
		saveUserProfile(state, value) {
			state.getUser.username = value.username
			state.getUser.email = value.email
			state.getUser.phone = value.phone
			state.getUser.avator = `https://www.chengzier.cn:8000/${value.avator}`
		},
		// 存储部门
		saveHead(state, value) {
			if (value == 'jishu') {
				state.area = '技术部'
			} else if (value == 'caiwu') {
				state.area = '财务部'
			} else if (value == 'houqin') {
				state.area = '后勤部'
			} else if (value == '营销') {
				state.area = '营销部'
			} else if (value == 'renshi') {
				state.area = '人事部'
			}
		},
		// 存储电话
		savePhone(state, value) {
			state.getUser.phone = value
		},
		// 储存email
		saveEmail(state, value) {
			state.getUser.email = value
		},
		// 存储用户名
		saveUsername(state, value) {
			state.getUser.username = value
		},
		// 退出登陆删除token
		DelectToken(state) {
			state.token = ''
		},
		// 点击新增标题  弹出框
		isdialogVisible(state) {
			state.dialogVisible = !state.dialogVisible
		},
		// Pop弹出框取消按钮
		cancelButton(state) {
			state.dialogVisible = false
		},
		// 点击编辑 弹出框
		addVisible(state) {
			state.addDialogVisible = true
		},
		// 编辑视频信息时，取消关闭窗口
		cancelButton1(state) {
			state.addDialogVisible = false
		},
		// 存储一级标题PK
		SAVESTORE(state, value) {
			// 给Vuex 存储一份PK
			state.pk = value
		}
	},
	actions: {
		// 调用获取用户信息 利用promise返回值判断token真伪
		getUserProfile({ commit, state }) {
			return new Promise((resolve, reject) => {
				userProfile(state.token).then(res => {
					if (!res.data.status) {
						commit('saveUserProfile', res.data)
						resolve()
					} else {
						reject()
					}
				})
			})
		},
		saveVideoName({ commit, state }) {
			return new Promise((reslove,rej)=>{
				getClassifyr.course(state.token).then((res) => {
					if (res.data.status == 'success') {
						commit('SAVESTORE', res.data.data)
						reslove()
					}
				})
			})
		}
	},
	modules: {
	}
})
